<?php

include_once 'actions.class.php';

class Pg_conn implements Actions {

    public $dbconn = null;
    public $error = false;

    function Pg_conn($dbconn) {
        $this->setDbconn($dbconn);
    }

    public function close($dbconn) {
        @pg_close($dbconn);
    }

    public function connect($stringConn) {
        $conn = @pg_connect($stringConn) or die('No se pudo establecer conexion con la base de datos');
        return $conn;
    }

    public function execute($sql) {
        $res = @pg_query($this->dbconn, $sql);
        if ($this->error && pg_last_error($this->dbconn)) {
            echo pg_last_error($this->dbconn);
            $this->close($this->dbconn);
            exit();
        }
        return $res;
    }

    public function fetchArrayAssoc($arr) {
        $rsp = array();
        while ($line = @pg_fetch_array($arr, null, PGSQL_ASSOC)) {
            array_push($rsp, $line);
        }
        @pg_free_result($arr);
        return ($rsp);
    }

    public function fetchArrayBoth($arr) {
        $rsp = array();
        while ($line = @pg_fetch_array($arr, null, PGSQL_BOTH)) {
            array_push($rsp, $line);
        }
        @pg_free_result($arr);
        return ($rsp);
    }

    public function fetchArrayNum($arr) {
        $rsp = array();
        while ($line = @pg_fetch_array($arr, null, PGSQL_NUM)) {
            array_push($rsp, $line);
        }
        @pg_free_result($arr);
        return ($rsp);
    }

    public function setDbconn($dbconn) {
        $this->dbconn = $dbconn;
    }

}

?>